package com.tutou.ikgroup.service.impl;

import com.tutou.ikgroup.mapper.ChatFriendMapper;
import com.tutou.ikgroup.mapper.ChatInitMapper;
import com.tutou.ikgroup.mapper.ChatMapper;
import com.tutou.ikgroup.pojo.po.Message;
import com.tutou.ikgroup.pojo.vo.ChatroomOfflineInfo;
import com.tutou.ikgroup.service.UserChatInit;
import com.tutou.ikgroup.util.ChangeType;
import com.tutou.ikgroup.util.UserCheck;
import com.tutou.ikgroup.websocket.ChannelContextUtil;
import com.tutou.ikuser.pojo.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.tutou.ikuser.util.RedisConstants.CHAT_OBO_KEY;
import static com.tutou.ikuser.util.RedisConstants.USER_LOGOUT_KEY;

@Service
public class UserChatInitImpl implements UserChatInit {
    @Autowired
    private ChatInitMapper chatInitMapper;

    @Autowired
    private ChatFriendMapper chatFriendMapper;

    @Autowired
    private ChatMapper chatMapper;

    @Autowired
    private ChannelContextUtil channelContextUtil;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    //获取未查看好友申请
    @Override
    public void getNewFriend(String userId) {
        List<String> friendRequest = chatFriendMapper.getFriendRequest(userId);
        if (friendRequest.size() == 0) {
            return;
        }
        Map<String, String> friend = new HashMap<>();
        chatFriendMapper.getFriendRequestNameByUserId(friendRequest).forEach(map -> {
            friend.put(map.get("sender"), map.get("message"));
        });
        channelContextUtil.getNewFriend(userId, friend);
    }


    //获取离线信息条数（谁发了多少条未读信息）
    @Override
    public void getOfflineInfo(String userId) {
        List<HashMap<String, Long>> offlineInfoByUserId = chatInitMapper.getOfflineInfoByUserId(userId);
        if (offlineInfoByUserId.size() == 0) {
            return;
        }
        Map<String, Long> chatOfflineInfo = new HashMap<>();
        offlineInfoByUserId.forEach(map -> {
            chatOfflineInfo.put(String.valueOf(map.get("sender")), map.get("messageCount"));
        });
        channelContextUtil.getOfflineInfo(userId, chatOfflineInfo);
    }

    //获取离线群聊信息（聊群有多少条未读信息）
    @Override
    public void getOfflineChatroomInfo(String userId) {
        //获取用户加入的群聊
        List<String> chatroomIds = chatMapper.getChatroomIdListByUserId(userId);
        if (chatroomIds.size() != 0) {
            List<HashMap<String, Long>> offlineInfoChatroom = chatInitMapper.getOfflineInfoChatroom(new ChatroomOfflineInfo(chatroomIds, userId));
            if (offlineInfoChatroom.size() == 0) {
                return;
            }
            Map<String, Long> offlineInfos = new HashMap<>();
            offlineInfoChatroom.forEach(map -> {
                offlineInfos.put(String.valueOf(map.get("roomName")), map.get("messageCount"));
            });
            channelContextUtil.getOfflineInfoChatroom(userId, offlineInfos);
        }
    }

    //读取之前的发送的好友申请有没有通过
    @Override
    public void getNewFriendPassOrReject(String userId) {
        List<String> newFriendPassOrReject = chatFriendMapper.getNewFriendPassOrReject(userId);
        channelContextUtil.getNewFriendPassOrReject(userId, newFriendPassOrReject);
    }
}
